/*
 * jQuery goToTop v1.0 beta 
 * Go to top of page jQuery Plugin
 * http://code.google.com/p/jquery-micro-plugins/
 *
 * Copyright © 2011 Alex Che
 * 
 * Open source under the Apache License 2.0 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

(function ($) {
    $.goToTop = function (options) {
        var defaults = {
            toTopElementId: 'go-to-top', // id of element that will be used as "go to top" link
            toTopText: '', // text of "go to top" link - void by default to use image instead of text
            toTopTitle: 'в начало страницы', // value of title attribute of "go to top" element
            hiddenHeight: 600, // height of scrolling while "go to top" element will be hidden
            speed: 1000, // speed of scrolling
            easing: 'easeOutQuint' // scrolling easing mode - values should be used with jQuery easing plugin, otherwise default 'swing' mode will be used
        },
            opts = $.extend(defaults, options),
            easing = ($.easing['jswing']) ? opts.easing : 'swing';
        $('<a title="' + opts.toTopTitle + '" id="' + opts.toTopElementId + '">' + opts.toTopText + '</a>').appendTo('body').click(function (e) {
            e.preventDefault();
            $('html, body').animate({
                scrollTop: 0
            }, opts.speed, easing);
        }).hover(function () {
            $(this).stop().animate({
                opacity: 0.7
            }, 300);
        }, function () {
            $(this).stop().animate({
                opacity: 1
            }, 300);
        });
        var $elem = $('#' + opts.toTopElementId);
        $(window).scroll(function () {
            var pos = $(window).scrollTop();
            if (!jQuery.support.hrefNormalized) {
                $elem.css({
                    'position': 'absolute',
                    'top': ($(window).scrollTop() + $(window).height() - 50) + 'px'
                });
            };
            if (pos >= opts.hiddenHeight) {
                $elem.fadeIn();
            } else {
                $elem.fadeOut(function () {
				    $(this).clearQueue();
				});
            };
        });
    };
})(jQuery);